package xmu.crms.controller;

import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import xmu.crms.vo.TopicVO;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author status200
 * @date 2017/12/26
 */
@Controller
public class WebSocketController {

    private final SimpMessagingTemplate simpMessagingTemplate;

    public WebSocketController(SimpMessagingTemplate simpMessagingTemplate) {
        this.simpMessagingTemplate = simpMessagingTemplate;
    }

    @SubscribeMapping("/seminar/{seminarId}/topics")
    public List<TopicVO> getAllAvailbleTopics(@DestinationVariable("seminarId") BigInteger seminarId) {

        List<TopicVO> vos = new ArrayList<>();

        TopicVO topic = new TopicVO();
        topic.setId(new BigInteger("257"));
        topic.setName("领域模型与模块");
        topic.setDescription("Domain model与模块划分");
        topic.setGroupLimit(5);
        topic.setGroupLeft(2);

        vos.add(topic);

        topic = new TopicVO();
        topic.setId(new BigInteger("258"));
        topic.setName("领域模型与模块1");
        topic.setDescription("Domain model与模块划分1");
        topic.setGroupLimit(5);
        topic.setGroupLeft(0);
        vos.add(topic);

        return vos;
    }

    @MessageMapping("/group/{groupId}/topic")
    public void chooseTopic(@DestinationVariable("groupId") BigInteger groupId,
                                     @Header("Authorization") String authString,
                            @Header("seminarId") BigInteger seminarId) {

        String destination = String.format("/seminar/%d/topics", seminarId);

        List<TopicVO> vos = new ArrayList<>();

        TopicVO topic = new TopicVO();
        topic.setId(new BigInteger("257"));
        topic.setName("领域模型与模块");
        topic.setDescription("Domain model与模块划分");
        topic.setGroupLimit(5);
        topic.setGroupLeft(0);

        vos.add(topic);

        topic = new TopicVO();
        topic.setId(new BigInteger("258"));
        topic.setName("领域模型与模块1");
        topic.setDescription("Domain model与模块划分1");
        topic.setGroupLimit(5);
        topic.setGroupLeft(0);
        vos.add(topic);

        simpMessagingTemplate.convertAndSend(destination,vos);
    }
}
